package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacRoleMenuMapper;
import com.csun.cmny.provider.model.domain.UacMenu;
import com.csun.cmny.provider.model.domain.UacRoleMenu;
import com.csun.cmny.provider.model.dto.group.GroupRoleDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.service.UacRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * The class Uac role menu service.
 *
 * @author paascloud.net@gmail.com
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UacRoleMenuServiceImpl extends BaseService<UacRoleMenu> implements UacRoleMenuService {
	@Resource
	private UacRoleMenuMapper uacRoleMenuMapper;

	@Override
	public int delRoleMenuList(Set<UacRoleMenu> uacRoleMenus) {
		int result = 0;
		for (UacRoleMenu uacRoleMenu : uacRoleMenus) {
			result += uacRoleMenuMapper.delete(uacRoleMenu);
		}
		return result;
	}

	@Override
	public void deleteByRoleId(Long roleId) {
		if (roleId == null) {
			throw new UacBizException(ErrorCodeEnum.UAC10012001);
		}
		UacRoleMenu roleMenu = new UacRoleMenu();
		roleMenu.setRoleId(roleId);
		uacRoleMenuMapper.delete(roleMenu);
	}

	@Override
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public List<UacRoleMenu> listByRoleId(Long roleId) {
		if (roleId == null) {
			throw new UacBizException(ErrorCodeEnum.UAC10012001);
		}
		UacRoleMenu roleMenu = new UacRoleMenu();
		roleMenu.setRoleId(roleId);
		return uacRoleMenuMapper.select(roleMenu);
	}

	@Override
	public void insert(Long roleId, Set<Long> menuIdList) {
		if (roleId == null) {
			throw new UacBizException(ErrorCodeEnum.UAC10012001);
		}
		UacRoleMenu uacRoleMenu = new UacRoleMenu();
		uacRoleMenu.setRoleId(roleId);
		for (Long menuId : menuIdList) {
			uacRoleMenu.setMenuId(menuId);
			uacRoleMenuMapper.insertSelective(uacRoleMenu);
		}
	}

	@Override
	public void deleteByRoleIdList(final List<Long> roleIdList) {
		uacRoleMenuMapper.deleteByRoleIdList(roleIdList);
	}

	/**
	 * batch save role menu
	 *
	 * @param roleId        the role id
	 * @param checkMenuList the check menu list
	 *
	 * @return the int
	 */
	@Override
	public int batchSave(Long roleId, List<Long> checkMenuList) {

		return uacRoleMenuMapper.batchSave(roleId, checkMenuList);
	}

	/**
	 * batch delete role menu
	 *
	 * @param roleId         the role id
	 * @param deleteMenuList the delete menu list
	 *
	 * @return the int
	 */
	@Override
	public int batchDelete(Long roleId, List<Long> deleteMenuList) {

		return uacRoleMenuMapper.batchDelete(roleId, deleteMenuList);
	}

	/**
	 * query list by role id list
	 *
	 * @param roleIdList the role id list
	 *
	 * @return the list
	 */
	@Override
	public List<UacRoleMenu> queryListByRoleIdList(List<Long> roleIdList) {

		return uacRoleMenuMapper.queryListByRoleIdList(roleIdList);
	}

	/**
	 * 查询角色所拥有的菜单列表
	 *
	 * @param groupRoleDto  the group role dto
	 *
	 * @return  the list.
	 */
	@Override
	public List<UacMenu> queryRoleMenuListWithCheck(GroupRoleDto groupRoleDto) {

		return uacRoleMenuMapper.queryRoleMenuListWithCheck(groupRoleDto);
	}

	/**
	 * 查询角色所拥有的菜单列表
	 *
	 * @param groupRoleDto the group role dto
	 *
	 * @return the list.
	 */
	@Override
	public List<UacMenu> queryRoleMenuList(GroupRoleDto groupRoleDto) {

		return uacRoleMenuMapper.queryRoleMenuList(groupRoleDto);
	}

	/**
	 * 查询指定角色的菜单Id列表
	 *
	 * @param roleId the role id
	 *
	 * @return the list.
	 */
	@Override
	public List<Long> queryMenuIdListByRoleId(Long roleId) {

		return uacRoleMenuMapper.queryMenuIdListByRoleId(roleId);
	}
}
